package com.cy.oa.service.impl;

import javax.annotation.Resource;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import com.cy.oa.service.ActService;
@Service
public class ActServiceImpl implements ActService {
	@Resource
	RuntimeService runtimeService;
	@Resource
	RepositoryService repositoryService;
	@Resource
	HistoryService historyService;

	@Override
	public ProcessDefinition getProcessDefinitionByInstanceId(String processInstanceId) {
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
				.processInstanceId(processInstanceId).singleResult();
		//如果审批完成，这里的流程实例为空,如果为空，去历史表查询
		String processDefinitionId = "";
		if(null==processInstance) {
			 HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
					 .processInstanceId(processInstanceId).singleResult();
			 processDefinitionId = historicProcessInstance.getProcessDefinitionId();
		}else {
			processDefinitionId = processInstance.getProcessDefinitionId();
		}	
		return repositoryService.getProcessDefinition(processDefinitionId);
	}

}
